package com.yichi.util;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.yichi.R;
import com.yichi.view.SpaceEditText;

/**
 * 自定义弹出框
 * Created on 2021/04/22.
 */
public class DialogUtil {

    private static Dialog dialog;

    /**
     * 不带输入框的回调
     */
    public interface MyCustomDialogListener {
        void ok();

        void no();
    }

    /**
     * 通用提示框
     *
     * @param context 上下文
     * @param title   弹窗的标题
     * @param message 弹窗的内容
     * @param mistake 错误提示
     * @param o       弹窗的接口回调，处理点击确认和取消的事件
     */
    public static void showSpaceEditDialog(Context context,
                                           final String title,
                                           final String message,
                                           final String mistake,
                                           final boolean Cancelable,
                                           final MyCustomDialogListener o) {
        final Dialog dialog = new Dialog(context, R.style.Translucent_NoTitle1);
        final View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_space_layout, null);
//    view.setOnClickListener(new View.OnClickListener() {
//      @Override
//      public void onClick(View v) {
//        dialog.dismiss();
//      }
//    });`  1
        TextView title_tx = view.findViewById(R.id.dialog_layout_title);
        TextView message_tx = view.findViewById(R.id.dialog_message);
        SpaceEditText spaceEditText = view.findViewById(R.id.space_edit);
        spaceEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                //密码监听
                if (editable.toString().length() == 5) {
//          String adminPsw = BaseSharePreferences.getBaseSharePreference(context).readAdminPassword();
                    String adminPsw = "12345";
                    if (adminPsw.equals(editable.toString())) {
                        o.ok();
                        dialog.dismiss();
                    } else {
                        message_tx.setText(mistake);
                        message_tx.setTextColor(Color.RED);
                    }

                } else if (editable.toString().length() < 5) {
                    message_tx.setText(message);
                    message_tx.setTextColor(Color.GRAY);
                }
            }
        });

        title_tx.setText(title);
        message_tx.setText(message);
        view.findViewById(R.id.dilog_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                o.no();
            }
        });


        // progressDialog.setMessage(msg);
//    if (!Cancelable) {
//      view.findViewById(R.id.dilog_close).setVisibility(View.GONE);
//    }
        dialog.setCanceledOnTouchOutside(Cancelable);
        dialog.setCancelable(Cancelable);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().addContentView(
                view,
                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));
        dialog.show();
    }

}